package cn.xxstudy.permission

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import cn.xxstudy.permission.help.PermissionHelper
import cn.xxstudy.permission.help.callback.PermissionResultCallback
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        call.setOnClickListener {

            PermissionHelper.init(this)
                .addPermissions(
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_CALENDAR,
                    Manifest.permission.READ_CALENDAR,
                    Manifest.permission.SEND_SMS
                )
                .addDeniedPermissionCallbackRetry()
                .addPermanentDeniedPermissionToSettings()
//                .request { allGranted, grantedPermissions, deniedPermissions, permanentDeniedPermissions ->
//                    Toast.makeText(this@MainActivity, "授权完成：$allGranted", Toast.LENGTH_SHORT).show()
//
//                }
                .request(object : PermissionResultCallback {
                    override fun callback(
                        allGranted: Boolean,
                        grantedPermissions: Array<String>,
                        deniedPermissions: Array<String>,
                        permanentDeniedPermissions: Array<String>
                    ) {
                        Toast.makeText(this@MainActivity, "授权完成：$allGranted", Toast.LENGTH_SHORT).show()
                    }
                }
                )

        }
    }

    private fun call() {
        val intent = Intent(Intent.ACTION_DIAL)
        val uri = Uri.parse("tel:18682188964")
        intent.data = uri
        startActivity(intent)
    }
}
